/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年4月14日 上午12:42:28
 */
package com.absir.appserv.system.service;

import java.nio.channels.SocketChannel;

import com.absir.appserv.system.helper.HelperEncrypt;
import com.absir.core.kernel.KernelObject;
import com.absir.server.in.InModel;
import com.absir.server.in.Input;
import com.absir.server.socket.InputSocketImpl;
import com.absir.server.socket.SocketChannelContext;

/**
 * @author absir
 *
 */
public class InputSocketMaster extends InputSocketImpl {

	/**
	 * @param input
	 * @return
	 */
	public static boolean onAuthentication(Input input) {
		if (input instanceof InputSocketMaster) {
			return true;
		}

		String slvky = input.getParam("slvky");
		if (slvky != null) {
			String[] slvkys = slvky.split("_");
			if (slvkys.length == 2) {
				String id = slvkys[0];
				SocketChannelContext channelContext = MasterService.ME.getMasterReceiverContext().getServerContext().getChannelContexts().get(id);
				if (channelContext != null && KernelObject.equals(MasterService.ME.getSlaveKey(id), slvky)) {
					if (input.getAddress().equals(channelContext.getSocketChannel().socket().getLocalAddress().getHostAddress())) {
						return true;

					} else {
						String slvhash = input.getParam("slvhash");
						if (slvhash != null && slvhash.equals(HelperEncrypt.encryptionMD5(slvky, MasterService.ME.getKey().getBytes()))) {
							return true;
						}
					}
				}
			}
		}

		return false;
	}

	/**
	 * @param model
	 * @param inputSocketAtt
	 * @param socketChannel
	 */
	public InputSocketMaster(InModel model, InputSocketAtt inputSocketAtt, SocketChannel socketChannel) {
		super(model, inputSocketAtt, socketChannel);
	}

}
